<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Product;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Callout;
use Encore\Admin\Widgets\Form as WidgetsForm;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;

class ProductsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('商品列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('商品详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑商品')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('创建商品')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Product);
        $grid->model()->with(['category']);
        $grid->id('ID')->sortable()->sortable();
        $grid->title('商品名称');
        $grid->column('category.name', '类目')->sortable();
        $grid->font_image('展示图')->lightbox(['width' => 50, 'height' => 50]);
        $grid->price('价格')->sortable();
        $grid->sold_count('销量')->sortable();
        $grid->stock('库存')->sortable();
        $grid->sort('排序')->sortable()->editable();
        // $grid->rating('评分');
        // $grid->review_count('评论数');
        $states = [
            'on' => ['text' => '是'],
            'off' => ['text' => '否'],
        ];
        $grid->column('产品状态')->switchGroup([
            'on_sale' => '上架',
            'is_hot' => '热门',
            'is_new' => '最新',
            'is_recommend' => '推荐',
        ], $states);
        $grid->column('SKU 列表')->expand(function ($model) {
            $skus = $model->skus()->take(10)->get()->map(function ($sku) {
                return $sku->only(['id', 'title', 'price', 'stock']);
            });
            return new Table(['ID', 'sku名', '价格', '库存'], $skus->toArray());
        });

        // $grid->column('SKU列表')->modal('sku列表', function ($model) {

        //     $skus = $model->skus()->take(10)->get()->map(function ($sku) {
        //         return $sku->only(['id', 'title',  'price', 'stock']);
        //     });
        //     return new Table(['ID', 'sku名',  '价格', '库存'], $skus->toArray());
        // });
        $grid->column('编辑sku')->display(function () {
            return "<a href=/admin/product_skus?pid=" . $this->id . "><div class='btn btn-sm btn-success'>编辑sku</div></a>";
        });
        $grid->disableExport();
        $grid->actions(function ($actions) {
            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->column(1 / 2, function ($filter) {
                $filter->like('title', '商品名称')->placeholder('请输入商品名称(支持模糊查询)。。。');
            });
            $filter->equal('on_sale', '上下架')->radio([
                '' => '所有',
                0 => '下架',
                1 => '上架',
            ]);
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Product::findOrFail($id));

        $show->id('Id');
        $show->title('商品名称');
        $show->profile('商品简介');
        $show->unit('单位名称');
        $show->description('商品详情');
        // $show->video_url('商品展示视频链接');
        $show->image('展示图')->image();
        $show->on_sale('是否上架')->using([0 => '否', 1 => '是']);
        $show->sold_count('销量');
        $show->price('价格');
        $show->created_at('提那家时间');
        $show->updated_at('最近更新');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Product);
        // 创建一个输入框，第一个参数 title 是模型的字段名，第二个参数是该字段描述
        $form->text('title', '商品名称')->rules('required');
        $form->text('profile', '商品简介');
        $form->text('unit', '单位名称')->rules('required');
        $form->number('sort', '排序');
        // 添加一个类目字段，与之前类目管理类似，使用 Ajax 的方式来搜索添加
        $form->select('category_id', '类目')->options(function ($id) {
            $category = Category::find($id);
            if ($category) {
                return [$category->id => $category->full_name];
            }
        })->options(Category::where('is_directory', 0)->pluck('name', 'id')); //ajax('/admin/api/categories?is_directory=0');
        // $form->text('video_url','商品展示视频链接');
        // 创建一个选择图片的框
        $form->image('font_image', '封面主图片')->rules('required|image');
        $form->multipleImage('carousel_image', '轮播展示图片')->removable()->help('可多选图片');
        // 创建一个富文本编辑器
        $form->editor('description', '商品描述')->rules('required');
        // 创建一组单选框
        $states = [
            'on' => ['value' => 1, 'text' => '上架', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '下架', 'color' => 'danger'],
        ];
        $form->switch('on_sale', '是否上架')->states($states);
        $h_states = [
            'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        ];
        $form->switch('is_hot', '热卖商品')->states($h_states);
        $r_states = [
            'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        ];
        $form->switch('is_recommend', '推荐商品')->states($r_states);
        $n_states = [
            'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        ];
        $form->switch('is_new', '是否新品')->states($n_states);
        $form->deleted(function () {
        });
        $form->tools(function (Form\Tools $tools) {
            // 去掉`查看`按钮
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
        });
        $form->saved(function (Form $form) {
            return redirect('/admin/product_skus/edit_sku?pid='.$form->model()->id);
        });
        return $form;
    }
}
